package blog.controller;
import blog.entity.Member;
import blog.service.DatabaseService;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Cell;
import jxl.JXLException;
import jxl.Sheet;
import jxl.Workbook;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value="master/*")
public class MemberController {

	@Autowired
	DatabaseService databaseService;
	
	//list Member
		@RequestMapping(value="member/searchMember")
		public String listAdmin(ModelMap modelMap){
			List<Member> mbr = databaseService.listMember();
			modelMap.put("member",mbr.toArray());
			return "/master/member/searchMember";
		}
		
		//Find by ID 
		@RequestMapping(value="member/detailMember", method=RequestMethod.GET)
		public String openMember(@ModelAttribute Member mbr, ModelMap modelMap){
			
			mbr = databaseService.findMemberById(mbr.getIdm());
			modelMap.put("member", mbr);
			return "/master/member/detailMember";
		}
		
		//update Member
		@RequestMapping(value="member/detailMember", method=RequestMethod.POST)
		public String updateMember(@ModelAttribute Member mbr){
			
			databaseService.saveMember(mbr);
			return "redirect:searchMember";
		}
		
		//insert Member
		@RequestMapping(value="member/insertMember")
		public String insertMember(){
			return "/master/member/insertMember";
		}
		
		@RequestMapping(value="member/insertMember", method = RequestMethod.POST)
		public String saveMember(@ModelAttribute Member mbr){
			databaseService.saveMember(mbr);
			return "redirect:searchMember";
		}
		
		//delete
		@RequestMapping(value="member/deleteMember")
		public String deleteMember(Member member){
			databaseService.deleteMember(member);
			return "redirect:searchMember";
		}
		
		@RequestMapping(value="member/upload")
		public String uploadExcel(@ModelAttribute Member mbr, 
				HttpServletRequest req, HttpServletResponse res)
				throws IOException, ServletException, JXLException {
			
			ServletInputStream is = req.getInputStream();
			byte[] junk = new byte[1024];
			
			is.readLine(junk, 0, junk.length);
			is.readLine(junk, 0, junk.length);
			is.readLine(junk, 0, junk.length);
			is.readLine(junk, 0, junk.length);
			
			Workbook workbook = Workbook.getWorkbook(is);
			
			Sheet s = workbook.getSheet(0);
			int row = s.getRows();
			int col = s.getColumns();
			Cell data = null;
			
			for(int i = 1; i < row; i++){
				for(int j = 1; j < col; j++){
					data = s.getCell(j, i);
					mbr.setUsername(data.getContents());
					mbr.setPassword(data.getContents());
					mbr.setIdm((Integer) null);
					databaseService.saveMember(mbr);
				}
			}
			return "redirect:searchMember";
		}

	
}
